package ctrls;

import ejb.GererConnectLocal;
import entities.Clients;
import java.util.logging.*;
import javax.naming.*;
import javax.servlet.http.*;

/**
 *
 * @author CDI215
 */
public class SctrlConnect implements ISsCtrl {

    GererConnectLocal con = lookupGererConnectBean();
    public static final String ATT_CLIENT = "client";
    public static final String ATT_FORM = "form";
    String url = "/WEB-INF/login.jsp";

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        Cookie cookiePresent = getCookie(request, ATT_CLIENT);

        if ("login".equals(request.getParameter("action"))) {
           url = "/WEB-INF/login.jsp";
        }
        
        if ("connexion".equals(request.getParameter("action"))) {
            Clients client = con.connect(request);
            request.setAttribute(ATT_CLIENT, client);
            request.setAttribute(ATT_FORM, con);
            Cookie compteur = getCookie(request, "compteur");
            if (compteur == null) {
                compteur = new Cookie("compteur", "1");
            }
            if (con.getErreurs().isEmpty()) {
                /* Si aucune erreur */
                if (request.getParameter("cc") != null) {
                    Cookie cc = new Cookie(ATT_CLIENT, "" + client.getId());
                    cc.setMaxAge(60 * 60);
                    response.addCookie(cc);
                }
                compteur.setMaxAge(0);
                response.addCookie(compteur);
                session.setAttribute(ATT_CLIENT, client);
                url = "/WEB-INF/home.jsp";
            } else {
                /* Sinon ré-affichage du formulaire avec les erreurs */
                session.setAttribute(ATT_CLIENT, null);
                int i = Integer.parseInt(compteur.getValue());
                compteur.setValue(++i + "");
                if (i > 3) {
                    request.setAttribute("tentative", true);
                }
                compteur.setMaxAge(3 * 60);
                response.addCookie(compteur);
                url = "/WEB-INF/login.jsp";
            }

        }

        if ("bye".equals(request.getParameter("action"))) {
            session.setAttribute(ATT_CLIENT, null);
            if (cookiePresent != null) {
                cookiePresent.setValue(null);
                cookiePresent.setMaxAge(0);
                response.addCookie(cookiePresent);
            }
            url = "/WEB-INF/home.jsp";
        }
        return url;
    }

    private GererConnectLocal lookupGererConnectBean() {
        try {
            Context c = new InitialContext();
            return (GererConnectLocal) c.lookup("java:global/LibWebEjb/LibWebEjb-ejb/GererConnect!ejb.GererConnectLocal");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private static Cookie getCookie(HttpServletRequest request, String nom) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie != null && nom.equals(cookie.getName())) {
                    return cookie;
                }
            }
        }
        return null;
    }
}
